/**
 * 
 * @authors youzhao.zhou@pandauu.com
 * @date    2016-10-26 12:30:06
 * @version $Id$
 *
 *
 * 快捷回复模块
 *    添加列表选择事件
 *    添加使用按钮事件
 *    添加监听回退按钮事件
 *    解除所有事件
 *    
 *
 */

;(function(root,factory){

  if(typeof exports === 'object' && typeof module === 'object'){
    
    factory(exports);

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    if (!root._s) {
      root._s = {};
    }
    factory(root);
  }
})(typeof window !== 'undefined' ? window : this ,function(exports, undefined){
  'use strict';

  function moduleInit($, log, conf, _, utils, options){

    var urlConf = conf._conf;       //存储配置信息
    var _ = _||{};       
    var pubsub = utils.pubSub()||{};
    var callback = {};
    var callbackFuc = utils.callbackFuc;

    /*
      对外传输的数据对象
    */
    var _data = {};


    pubsub.addEvent('getQuickData', function(data){
      if (data.code === 0) {
        renderQuick(data.data);
      }
    });
    pubsub.addEvent('rendered', function(data){
      quickC(data);
    });

    function receiver(identifier, options){
      var deferr = null;
      switch(identifier){
        case 'quick':
          deferr = getQuickData(options);
          deferr.then(function(data){
            /*
              回调通知数据获取完毕

            */
            pubsub.fireEvent('getQuickData',data);
          });
        break;
      }
      
      deferr.then(function(data){
        /*
          回调通知数据获取完毕
        */
        pubsub.fireEvent(identifier,data);
      });
    };

    /*
      M
    */
    /*
      获取快捷回复列表
    */
    function getQuickData(options){

      var deferr = $.ajax({
        url: urlConf.apiRoot + urlConf.apiPre.one + urlConf.api.quickReplyList,
        type: 'GET',
        dataType: 'json'
      })
      .done(function(data) {
        if (data.code === 0) {

        }
          
      })
      .fail(function() {
        log._log("error");
      })
      .always(function() {
        log._log("complete");
      });
      
      return deferr;
    }

    /*
      V
    */
    /*
      渲染快捷回复
    */
    function renderQuick(data){

      var tpl = $('#replyTpl');

      $('.jsReplyContainer').append(_.template(tpl.html())({list: data}));
      
      $('body')[0].scrollTop = 0;

      pubsub.fireEvent('rendered',{code:0});
      return {code:0};
    }

    /*
      C
    */

    function quickC(){
      var selectedDom = null;
      /*
        设置导航头部
      */
      utils.setHTML('.middle', '快捷回复列表');
      log._log('renderedQuick-finished');

      $('.jsService').addClass('s-hide');
      $('.jsQuickreply').removeClass('s-hide');
      window.history.pushState('quick','quick','#/quick');

      $('.jsQuickreply').on('click', quickOp);


      window.addEventListener('popstate',backEvent);
    }

    /*
      快捷回复操作
    */
    function quickOp(event){

      event.preventDefault();
      var target = $(event.target);
      var eventType = target.data('event');

      switch(eventType){
        case 'li':
          target.siblings(".select").removeClass('select');
          target.addClass('select');
          _data.selectedDom = target;
          _data.text = _data.selectedDom.text().trim();
          break;
        case 'use':

          if (!_data.selectedDom) {
            alert('请选择快捷回复');

            return false;
          }

          log._log(_data);
          pubsub.fireEvent('getData',_data);
          /*
            设置导航头部
          */
          $('.middle').html('熊猫天下客服');
          window.history.back();

          break;
      }

    }

    /*
      回退操作
    */

    function backEvent(){
      $('.jsQuickreply').unbind('click', quickOp);
      $('.jsService').removeClass('s-hide');
      $('.jsQuickreply').addClass('s-hide');

      setTimeout(function(){
        $('body')[0].scrollTop = $('.jsService').height()-$(window).height();
        window.removeEventListener('popstate',backEvent);
      },0);
      
    }


    return {
      set: function(identifier, callbacks){
        callback[identifier] = [callbacks];

        pubsub.addEvent(identifier, function(data){
          console.log(callback[identifier]);
          callbackFuc(callback[identifier],data);
        });
      },
      start: function(identifier, options){
        receiver(identifier, options);
      }
    };
  }

  exports.quick = moduleInit;

});